package cn.lb.org.controller;


import cn.lb.annotation.LBPermission;
import cn.lb.org.domain.Employee;

import cn.lb.org.query.EmployeeQuery;
import cn.lb.org.service.IEmployeeService;
import cn.lb.permission.dto.EmployeeRoleDTO;
import cn.lb.basic.util.PageList;
import cn.lb.basic.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@LBPermission(name = "员工管理",descs = "关于员工的一些操作")
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;


    @LBPermission(name = "员工新增修改")
    @PutMapping
    public R addAndsave(@RequestBody Employee employee){
        try {
            if(Objects.isNull(employee.getId())){
                employeeService.insert(employee);
            }else {
                employeeService.update(employee);
            }
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
    }

    @LBPermission(name = "员工删除")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable("id")Long id){
        try {
            employeeService.deleteById(id);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
    }
    @LBPermission(name = "员工通过ID查询")
    @GetMapping("/{id}")
    public R selectById(@PathVariable("id")Long id){
        try {
            Employee employee = employeeService.selectById(id);
            return R.me().setData(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
    }
    @LBPermission(name = "员工查询所有")
    @GetMapping
    public R selectAll(){
        try {
            List<Employee> employees = employeeService.selectAll();
            return R.me().setData(employees);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
    }
    @LBPermission(name = "员工分页和高级查询")
    @PostMapping
    public R page(@RequestBody EmployeeQuery query){
        try {
            PageList<Employee> page = employeeService.page(query);
            return R.me().setData(page);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("分页查询失败"+e.getMessage());
        }
    }
    @LBPermission(name = "员工批量删除")
    @PatchMapping
    public R batchdelete(@RequestBody List<Long> ids){
        try {
            employeeService.deletebatch(ids);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("分页查询失败"+e.getMessage());
        }
    }
    @LBPermission(name = "查询部门树")
    @GetMapping("/tree")
    public R tree(){
        try {
            List<Employee> employees = employeeService.trees();
            return R.me().setData(employees);
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
    }


    @LBPermission(name = "添加角色")
    @PutMapping("/role")
    public R setRole(@RequestBody EmployeeRoleDTO dto){
        try {
            System.out.println(dto);
            employeeService.setrole(dto);
            return R.me();
        } catch (Exception e) {
            e.printStackTrace();
            return R.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }
    }



}
